package com.xiaoma.bingox.webservice;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.xiaoma.bingox.entity.Test1;
import com.xiaoma.bingox.entity.XmlTest;
import com.xiaoma.bingox.service.CommonService;
import com.xiaoma.bingox.service.TestDalService;

@Controller
@RequestMapping("/test")
public class TestController {
    private Logger LOG = LoggerFactory.getLogger(TestController.class);
    
    @Autowired
    private TestDalService testDalService;
    
    @Autowired
    private CommonService commonService;
    

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findAll(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("tests", testDalService.findAll());
        LOG.info("test findall");
        return result;
        
    }
    
    @RequestMapping(value = "/tt" ,method = RequestMethod.GET)
    @ResponseBody
    public JSONObject tt(){
        JSONObject s = new JSONObject();
        s.put("s", "sssss");
        s.put("age", 1);
        s.put("t", null);
        Test1 t1 = new Test1();
        t1.setAge(22);
        t1.setName("xxx");
        JSONObject p = new JSONObject();
        p.put("tt", s);
        p.put("test1", t1);
        return p;
    }
    
    @RequestMapping(value = "/page/{curPage}/{perPage}" ,method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> page(@PathVariable Integer curPage, @PathVariable Integer perPage){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("tests", testDalService.page(curPage, perPage));
        return result;
    }
    
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> insert(){
        Test1 test = new Test1();
        test.setAge(30);
        test.setName("kent");
        testDalService.save(test);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("status", 1);
        return result;
    }
    
    
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> insert(@PathVariable Integer id){
        
        testDalService.delete(id);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("status", 1);
        return result;
    }
   
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> update(){
        Test1 test = new Test1();
        test.setId(1);
        test.setName("ppp");
        testDalService.update(test);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("status", 1);
        return result;
    }
    
    @RequestMapping(value = "/111", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> m111(){
        String url = "http://center.xiaomatuofu.com/tuofu21/getAliPayInfo.action";
        JSONObject json = commonService.postJson(url, null);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("status", 1);
        result.put("result", json);
        return result;
    }
    
    @RequestMapping(value = "/222", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> m222(){
        String url = "http://tpp.xiaoma.com/xmpg/service/applyTicket?productionName={a}&count={b}";
        JSONObject json = commonService.getJson(url, "XMTOEFL_21", 1);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("status", 1);
        result.put("result", json);
        return result;
    }
    
    @RequestMapping(value = "/333", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> m333(){
        String url = "http://center.xiaomatuofu.com/tuofu21/findOrder.action";
        MultiValueMap<String, String> data = new LinkedMultiValueMap<String, String>();
        data.set("orderNum", "1444724965832326");
        JSONObject json = commonService.postForm(url, data);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("status", 1);
        result.put("result", json);
        return result;
    }
    
    @RequestMapping(value = "/xml", method = RequestMethod.GET)
    @ResponseBody
    public XmlTest testXml(){
        XmlTest xt = new XmlTest();
        xt.setDate(new Date());
        xt.setId(1);
        xt.setName("aa");
        xt.setLastName("bbbbb");
        return xt;
    }
    
    
    
    
    public static void main(String args[]) {
    }
}
